package com.test.schoolhelper.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.test.schoolhelper.R;
import com.test.schoolhelper.base.BaseActivity;
import com.test.schoolhelper.databinding.ActivityAddScheduleBinding;
import com.test.schoolhelper.utils.CalendarReminderUtils;
import com.test.schoolhelper.utils.ToastUtils;
import com.test.schoolhelper.utils.Tools;

import java.util.Date;
import java.util.List;

// TODO: 2024/5/31 日程添加 
public class AddScheduleActivity extends BaseActivity<ActivityAddScheduleBinding> implements View.OnClickListener {

    private String title, comment;
    private String nDate, startTime, endTime;

    @Override
    protected void initData() {
        super.initData();
        nDate = Tools.customFormat(new Date(), "yyyy-M-d HH:mm");
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().tvSave.setOnClickListener(this);
        getBinding().rlStart.setOnClickListener(this);
        getBinding().rlEnd.setOnClickListener(this);
        getBinding().ivBack.setOnClickListener(this);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_add_schedule;
    }

    @Override
    protected ActivityAddScheduleBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityAddScheduleBinding.inflate(layoutInflater);
    }

    // TODO:请求日历权限
    private void requestCALENDARPermission() {
        XXPermissions.with(this)
                .permission(Permission.Group.CALENDAR)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                        if (allGranted) {
                            handler.sendEmptyMessage(0);
                        }
                    }

                    @Override
                    public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {

                    }
                });
    }

    // TODO:设置开始/结束时间
    private void showDateDialog(int type) {
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(this, (date, v) -> {
            if (type == 1) {
                startTime = Tools.customFormat(date, "yyyy-M-d HH:mm");
                getBinding().tvStart.setText(startTime);
            } else if (type == 2) {
                endTime = Tools.customFormat(date, "yyyy-M-d HH:mm");
                getBinding().tvEnd.setText(endTime);
            }
        }).setType(new boolean[]{true, true, true, true, true, false})//控制年-月-日-时-分-秒
                .build();
        pvTime.show();
    }

    Handler handler = new Handler(msg -> {
        switch (msg.what) {
            case 0:
                title = getBinding().etTitle.getText().toString();
                comment = getBinding().etComment.getText().toString();
                if (TextUtils.isEmpty(title)) {
                    ToastUtils.getInstance().show(this, "请输入日程标题");
                } else {
                    if (TextUtils.isEmpty(startTime)) {
                        startTime = nDate;
                    }
                    if (TextUtils.isEmpty(endTime)) {
                        endTime = nDate;
                    }
                    CalendarReminderUtils.addCalendarEvent(this, title, comment, Tools.strToLong(startTime, "yyyy-M-d HH:mm"), Tools.strToLong(endTime, "yyyy-M-d HH:mm"));
                    new Handler().postDelayed(() -> {
                        ToastUtils.getInstance().show(this, "添加成功");
                        finish();
                    },500);
                }
                break;
        }
        return false;
    });

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_save:
                requestCALENDARPermission();
                break;
            case R.id.rl_start:
                showDateDialog(1);
                break;
            case R.id.rl_end:
                showDateDialog(2);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}